﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WaitingLine
{
    class Program
    {
        static void Main(string[] args)
        {
            Line<int> line = new Line<int>(5);
            for (int i = 1; i < 6; i++)
            {
                line.Push(i);
            }
            line.Show();
            Console.WriteLine();
            for (int j = 0; j < 2; j++)
            {
                Console.WriteLine(line.Pop());
            }
            line.Show();
            Console.WriteLine();
            line.Push(10);
            line.Show();
            Console.WriteLine();
            line.Clear();
            line.Push(100);
            line.Show();
        }
    }

    class Line<T> //where T:class
    {
        private T[] arrayOfFive;
        private int counterForElements;
        T returnValue;

        public Line(int size)
        {
            arrayOfFive = new T[size];
            counterForElements = arrayOfFive.Length;
        }

        public void Push(T value)
        {
            if (counterForElements > 0)
            {
                arrayOfFive[--counterForElements] = value;
                if (counterForElements == 0)
                {
                    counterForElements = arrayOfFive.Length;
                }
            }
        }

        public T Pop() //last in last out
        {
            returnValue = arrayOfFive[arrayOfFive.Length - 1];
            for (int i = arrayOfFive.Length-1; i > 0; i--)
            {
                if (i > 0)
                {
                    arrayOfFive[i] = arrayOfFive[i - 1];
                }
            }
            arrayOfFive[arrayOfFive.Length-counterForElements] = default(T);
            counterForElements--;
            return returnValue;
        }

        public void Clear()
        {
            for (int i = 0; i < arrayOfFive.Length; i++)
            {
                arrayOfFive[i] = default(T);
            }
            counterForElements = 5;
        }

        public void Show()
        {
            for (int i = 0; i < arrayOfFive.Length; i++)
            {
                Console.Write("{0}  ", arrayOfFive[i]);
            }
        }
    }
}
